<?php
 /*
  *总后台商品模型类
  */
namespace Home\Model; 
use Think\Model;
class ItemModel extends Model{
	
	public function index($arg=array())
	{	
		$page=!empty($arg['page'])?intval($arg['page']):1;           
		//where 语句 
		$condition='';
		if(!empty($arg['typeid'])){
			$condition='typeid='.$arg['typeid'];
		}else{
			$condition.='1=1';
		}
		if(!empty($arg['brandid'])){
			$condition.=' AND brandid='.$arg['brandid'];
		}
		if(!empty($arg['keywords'])){
			$condition.=" AND itemname like '%".$arg['keywords']."%' or bn like '%".$arg['keywords']."%'";
		}
		if (!empty($arg['platid']))
		{
			$condition.=" AND platid !=0 ";
		}else{
			$condition.=' AND platid =0 ';
		}
		$count=M()->query("select count(*) as count from ".C(DB_PREFIX).'item where '.$condition);
 		//$count=M('item')->where('isdel=0')->count();
 		$pagearr= getPageArr($page,C('PAGESIZE'),$count[0]['count']);
 		$result=M('item')->where($condition)
 						 ->limit($pagearr['start'],$pagearr['pagesize'])
 		                 ->order('examine ASC,itemid DESC')->getField('itemid,itemname,bn,price,inventory, FROM_UNIXTIME(createtime, "%Y-%m-%d %H:%i:%S") createtime,supperprice,yid,examine,status,platid');
 		//判断商品是否存在多规格
		if(!empty($result))
		{	$product=M('product');
			$yun=M('yunying_user');
			foreach ($result as $key =>&$value) 
			{
				$my_count=$product->where('itemid=%d',$value['itemid'])->count();
				$yid=M('item')->where('itemid=%d',$value['itemid'])->getField('yid');
				$value['yname']=$yun->where('yid=%d',$yid)->getField('companyname');
				$value['isspeci']=!empty($my_count)?1:0; //是否多规格
			}

		}
 		$temp['pagearr']=$pagearr;
 		$temp['item']=!empty($result)?$result:'';
 		//设置page cookie
 		cookie('page',$page,120);
 		return $temp;

	}

}